from nptdms import TdmsFile, TdmsWriter, RootObject, GroupObject, ChannelObject
import numpy as np

# 读取文件
file1 = TdmsFile.read("file1.tdms")
file2 = TdmsFile.read("file2.tdms")

# 创建新文件
with TdmsWriter("combined.tdms") as tdms_writer:
    # 写入根对象属性（可选）
    root = RootObject(properties=file1.properties)
    tdms_writer.write_segment([root])

    # 遍历所有组
    for group_name in file1.groups():
        group1 = file1[group_name]
        group2 = file2[group_name]

        # 创建组对象
        group = GroupObject(group_name, properties=group1.properties)
        tdms_writer.write_segment([group])

        # 遍历所有通道
        for channel_name in group1.channels():
            channel1 = group1[channel_name]
            channel2 = group2[channel_name]

            # 拼接数据
            combined_data = np.concatenate((channel1[:], channel2[:]))

            # 创建并写入通道
            channel = ChannelObject(group_name, channel_name, combined_data,
                                    properties=channel1.properties)
            tdms_writer.write_segment([channel])